let casual = require('casual').en_US;
var moment = require('moment');
//https://github.com/boo1ean/casual
//npm install casual
//node random_data.js > db.json

casual.define('_note', function( _index) {
    var start = moment('2016-06-20'); 
	return {
        index:_index,
        content: casual.sentences(n = casual.integer(from = 1, to = 3)) ,
        addDate: start.add( casual.integer(from = 0, to = 480), 'day').format('MM/DD/YYYY'),
		isSetTop: casual.boolean,
	};
});


var imagelist = [
   'https://images-na.ssl-images-amazon.com/images/I/8158K8LN-ML._UX64_.jpg',
   'https://images-na.ssl-images-amazon.com/images/I/A1vjqBAZEzL._UX64_.jp',
   'https://images-na.ssl-images-amazon.com/images/I/71wltr8c92L._UX64_.jpg'
];

var product_status = [
    'Normal',
    'Processing',
    'Alert',
    'Archieve'
];

var asinList = [
    'B073CWLT5L',
    'B071FZQRC2',
    'B073TCG35H',
    'B06XC7CYXF',
    'B073NYLW7V',
    'B07125LC1Y'
];


let operation_list = [">", "=","<"];


casual.define('_condition', function( index) {

    var _id = casual.integer(from = 1, to = 5);
    var _operation ;
    var _value;
    var _component;

    if ( _id >=1 && _id <=4 ) {
        _operation = operation_list[ casual.integer( from = 0, to=2)];
        _value = casual.integer( from = 100, to=50000);
    } else {
        _operation = null; 
        _value = casual.boolean;
    }

    if ( index == 0 ) {
        _component = "null";
    } else {
        if ( casual.boolean) {
            _component = "and";
        }
        else {
            _component = "or";
        }
    }


    return {
        id: _id,
        operation: _operation,
        value : _value,
        component: _component,
    };

});

casual.define('_product', function( index) {

    var n = casual.integer( from = 0 , to = 3);
    var _conditions = new Array();
    for ( var i = 0 ; i < n ; i++ )
    {
       _conditions.push( casual._condition(i));     
    }

	return {
        id: index,
        imagesUlr: imagelist[casual.integer(from =0, to = 2)],
        title: casual.sentences(n = casual.integer(from = 1, to = 4)) ,
        seller: casual.word,
        status: product_status[casual.integer(from=0,to =3)],
        BSR: casual.integer(from=100,to =20000),
        price: casual.double( from=0.99, to=99.99).toFixed(2),
        date: casual.date,
        asin: asinList[casual.integer(from = 0, to=(asinList.length-1))],
        conditions:_conditions,
	};
});


function ToKeepTime( _date) {
    var starttime = new Date("2011-01-01");
    var total = (_date.getTime() - starttime.getTime())/60000;

    return Math.ceil(total); 
};

//product_detail start
casual.define( '_rank', function() {
    var dateCount = 180;
    var start = moment('2017-10-20'); 
    var rankstart = 1001;
    var cvs_ranks = [];
    

    for(var i = 0; i < 180; i++ ) {
        var b = start.clone().subtract(180-i, 'day').format('MM/DD/YYYY');
        var theDate = ToKeepTime( new Date(b));
        cvs_ranks.push( theDate);
        rankstart = rankstart + casual.integer( from = 100, to = 20000);
        cvs_ranks.push( rankstart);
    } 

    return cvs_ranks;
}
);


casual.define('_product_detail', function() {
    return {
    asin: "B01ER9G97W",
    domainId: 1,
    title: "EZYKOO Cargo Nets 39 X 27 inch Nylon Rear Cargo Organizer Mesh Flexible SUV Cargo Net Mesh for Truck SUV Cara",
    trackingSince: 2819591,
    lastUpdate: 3329340,
    lastPriceChange: 3313868,
    lastRatingUpdate: 3329352,
    imagesCSV: "61WF%2B-wDbKL.jpg,51haLXQEFLL.jpg,51kppvhbGQL.jpg,61-PPW0%2BGjL.jpg,61QVixCLdXL.jpg,61LawBSb7BL.jpg,41agxn7YPOL.jpg",
    rootCategory: 15684181,
    categories: [
      3147767011
    ],

    manufacturer: "Ecooltek",
    brand: "EZYKOO",
    label: "Ecooltek",
    publisher: "Ecooltek",
    productGroup: "Automotive Parts and Accessories",
    partNumber: "AT-CN-BK-K",
    studio: "Ecooltek",
    model: "EZYKOO",
    publicationDate: -1,
    releaseDate: -1,
    color: null,
    size: "39*27",
    packageHeight: 44,
    packageLength: 168,
    packageWidth: 137,
    packageWeight: 136,
    packageQuantity: 100,
    isAdultProduct: false,
    isEligibleForTradeIn: false,
    isEligibleForSuperSaverShipping: false,
    productType: 0,
    from:0,
    cvs_ranks: casual._rank,
    cvs_ratings: [
        3104896,
        40,
        3115978,
        39,
        3139336,
        38,
        3160900,
        39,
        3168068,
        38,
        3187752,
        37,
        3199468,
        38,
        3257204,
        37,
        3276356,
        38,
        3315288,
        39,
        3317636,
        40,
        3321832,
        41,
        3322950,
        40
      ],
    
    cvs_reviews: [
      3104896,
      57,
      3110468,
      58,
      3115978,
      61,
      3121052,
      62,
      3129652,
      67,
      3139336,
      68,
      3153756,
      74,
      3160900,
      76,
      3168068,
      81,
      3178168,
      83,
      3183932,
      85,
      3187752,
      88,
      3199468,
      89,
      3201120,
      90,
      3205088,
      89,
      3206448,
      90,
      3213560,
      91,
      3218300,
      92,
      3220724,
      93,
      3227036,
      94,
      3229730,
      96,
      3248120,
      97,
      3257204,
      98,
      3258522,
      99,
      3262444,
      101,
      3263952,
      102,
      3264480,
      103,
      3272744,
      102,
      3274108,
      104,
      3276356,
      105,
      3278722,
      106,
      3282778,
      105,
      3291592,
      107,
      3293966,
      108,
      3296488,
      110,
      3309548,
      111,
      3315288,
      117,
      3317636,
      122,
      3321832,
      146,
      3322950,
      127,
      3324464,
      128,
      3329340,
      129
     ],
    cvs_prices: [
        2819591,
        1099,
        2830748,
        1029,
        2841571,
        999,
        2856756,
        1099,
        2857396,
        999,
        2880060,
        1099,
        2881562,
        1199,
        2883540,
        1099,
        2887264,
        899,
        2887674,
        999,
        2915868,
        899,
        2916268,
        999,
        2917984,
        1029,
        2921688,
        1039,
        2924584,
        1029,
        2932352,
        999,
        2942364,
        1029,
        2943056,
        999,
        2968460,
        1019,
        2978296,
        1029,
        2993316,
        1039,
        3006588,
        -1,
        3013746,
        998,
        3016882,
        899,
        3030512,
        1039,
        3035674,
        999,
        3049604,
        1039,
        3063668,
        1049,
        3093240,
        999,
        3101830,
        1049,
        3132696,
        999,
        3149484,
        1029,
        3156688,
        1049,
        3171132,
        999,
        3172920,
        1499,
        3173844,
        1299,
        3174922,
        1199,
        3187750,
        1239,
        3200024,
        1169,
        3202948,
        1199,
        3205988,
        1189,
        3208640,
        1199,
        3215698,
        1229,
        3219094,
        1099,
        3224760,
        1199,
        3228664,
        1159,
        3229104,
        1179,
        3230308,
        1159,
        3233172,
        1199,
        3234392,
        1149,
        3243572,
        1099,
        3248818,
        1129,
        3263812,
        999,
        3268956,
        1029,
        3282336,
        1049,
        3296098,
        1029,
        3305782,
        1049,
        3309152,
        999,
        3310632,
        1029,
        3311096,
        1000,
        3313868,
        1039
       ],




    };

});



var theDB = {
    product_notes:[],
    product_list:[],
    product_detail:casual._product_detail,
};

for(var i=0; i<=25; i++){
    var note = casual._note(i);
    theDB.product_notes.push( note);
}


 for(var i=0; i<=100; i++){
     var pro = casual._product(i);
     theDB.product_list.push( pro);
 }

console.log( JSON.stringify(theDB) );


